package nl.altenpts.net.broker;

import java.nio.ByteBuffer;
import java.security.SignatureException;

/**
 * An incoming request
 * 
 * @author fabian
 *
 */

public class IncomingRequest {
	
	/**
	 * Constructs an incoming request with.
	 * 
	 * @param message The incoming message of the request
	 * @param requestid requestid
	 * @param broker broker with which the response wil be sent
	 */
	public IncomingRequest(IncomingMessage message, long requestid, SignedMessageBroker broker) {
		this.message = message;
		this.requestid = requestid;
		this.broker = broker;
	}
	
	/**
	 * Send message as response
	 * 
	 * @param message the response
	 * @throws SignatureException when signing the message fails
	 */
	public void respond(OutgoingMessage message) {
		ByteBuffer buffer = ByteBuffer.allocate(9);
		
		buffer.put(RequestBroker.REQUESTBROKER_RESPONSE);
		buffer.putLong(requestid);
		buffer.flip();
		message.prependContent(buffer);
		broker.sendMessage(message);		
	}
	
	/**
	 * Returns the request message
	 * 
	 * @return the request message
	 */
	public IncomingMessage getMessage() {
		return message;
	}
	
	private long requestid;
	private IncomingMessage message;
	private SignedMessageBroker broker;

}
